package com.example.lotterysystem.service.operater;

import com.example.lotterysystem.controller.param.ActivityPrize;
import com.example.lotterysystem.dao.dataobject.ActivityDO;
import com.example.lotterysystem.dao.mapper.ActivityMapper;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.example.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/2/3
 */
@Component
public class ActivityOperator extends AbstractActivityOperator {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if (activityId == null ||
                targetStatus == null) {
            return false;
        }
        ActivityDO activityDO = activityMapper.selectActivityById(activityId);
        if (activityDO == null) {
            return false;
        }
        //如果传入参数和数据库中的状态一致就不修改
        if (targetStatus.name().equalsIgnoreCase(activityDO.getStatus())) {
            return false;
        }
        int amount = activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if (amount > 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
